/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i1,st,ed) for(int i1 = st,j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) foR( i , st , ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip( X ) sleep( X )
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class PalindromizationDiv1 {
public:
    int getMinimumCost(string word, vector <string> operations);
};
inline vector<string> _split(string str) {
    stringstream ss(str);
    vector<string> vt;
    for (string tmp; getline(ss,tmp,' '); ) {
        if ( LN(tmp)<1 )continue;
        vt.pb(tmp);
    }
    return vt;
}
inline Int toI(string s) {
    Int d;
    sscanf(s.c_str(),"%lld",&d);
    return d;
}
const Int inf = 1023456789;
Int cost1[ 27 ][ 27 ];
Int COST[ 27 ][ 27 ];
string line;
Int dp[52][52];


inline void ini() {
    SET(dp,-1);
    fo(i,27) {
        fo(j,27) {
            cost1[i][j]= i==j ? 0 : inf;
        }
    }
}

Int go(int a, int b ) {
    if ( a>=b ) {
        return 0;
    }
    Int &res = dp[a][b];
    if ( res!=-1 ) {
        return res;
    }
    res = inf;
    int i = line[a]-'a';
    int j = line[b]-'a';


    Int x = COST[i][j]+go(a+1,b-1);
    Int y = COST[i][26]+go(a+1,b);
    Int z = COST[j][26]+go(a,b-1);

    res =  min( x, min(y, z ) );
    return res;
}

int PalindromizationDiv1 :: getMinimumCost(string word, vector <string> operations) {
    line = word;
    ini();

    fo(i, SZ(operations)) {
        vector<string> vi = _split( operations[i] );
        if ( vi[0]=="add" ) {
            int id = vi[1][0]-'a';
            Int c = toI(vi[2]);
            cost1[26][ id ] = c;
        }
        else if ( vi[0]=="erase" ) {
            int id = vi[1][0]-'a';
            Int c = toI(vi[2]);
            cost1[ id ][26] = c;
        }
        else {
            int id1 = vi[1][0]-'a';
            int id2 = vi[2][0]-'a';
            Int c = toI( vi[3]);
            cost1[ id1 ][ id2 ] = c;
        }
    }

    // floyed warshall
    fo(k,27) {
        fo(i,27) {
            fo(j,27) {
                Int c = cost1[i][k]+cost1[k][j];
                cost1[i][j] = min( cost1[i][j], c );
            }
        }
    }
    fo(i,27) {
        fo(j,27) {
            Int &z = COST[i][j];
            z = inf;
            fo(k,27) {
                Int a = cost1[i][k];
                Int b = cost1[j][k];
                if ( a+b < z ) {
                    z = a+b;
                }
            }
        }
    }


    SET(dp,-1);
    Int var = go( 0, LN(line)-1 );

    return var <inf ? (int)var : -1;

}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, string p0, vector <string> p1, bool hasAnswer, int p2) {
    cout << "Test " << testNum << ": [" << "\"" << p0 << "\"" << "," << "{";
    for (int i = 0; int(p1.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p1[i] << "\"";
    }
    cout << "}";
    cout << "]" << endl;
    PalindromizationDiv1 *obj;
    int answer;
    obj = new PalindromizationDiv1();
    clock_t startTime = clock();
    answer = obj->getMinimumCost(p0, p1);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p2 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p2;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    string p0;
    vector <string> p1;
    int p2;

    {
        // ----- test 0 -----
        p0 = "racecar";
        p1.clear() /*{}*/;
        p2 = 0;
        all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        p0 = "topcoder";
        string t1[] = {"erase t 1","erase o 1","erase p 1","erase c 1","erase d 1","erase e 1","erase r 1"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 5;
        all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        p0 = "topcoder";
        string t1[] = {"erase t 10","erase o 1","erase p 1","erase c 1","erase d 1","erase e 1","erase r 1"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 7;
        all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        p0 = "caaaaaab";
        string t1[] = {"change b a 100000","change c a 100000","change c d 50000","change b e 50000","erase d 50000","erase e 49999"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = 199999;
        all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        p0 = "moon";
        string t1[] = {"erase o 5","add u 7","change d p 3","change m s 12","change n d 6","change s l 1"};
        p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
        p2 = -1;
        all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
